1817C - Similar Polynomials - CodeForces Solution


combinatorics math *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int ll
#define fi first
#define se second
#define Mp make_pair
#define pb push_back
#define SZ(a) (int(a.size()))

typedef long long ll;
typedef double db;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
#define debug(...) fprintf(stderr, __VA_ARGS__)
std::mt19937_64 gen(std::chrono::system_clock::now().time_since_epoch().count());
ll get(ll l, ll r) { std::uniform_int_distribution<ll> dist(l, r); return dist(gen); }

constexpr int N = 2500100, P = 1e9 + 7;

int d, fac[N], inv[N], a[N], b[N];
int qpow(int a, int b) {
    int res = 1;
    while(b) {
        if(b & 1) res = res * a % P;
        a = a * a % P, b >>= 1;
    }
    return res;
}
int C(int n, int m) {
    if(n < 0 || m < 0 || n < m) return 0;
    return fac[n] * inv[m] % P * inv[n - m] % P;
}
int f(int arr[], int k, int x) {
    int res = 0;
    for(int i = 0; i <= k; i++) {
        int coef = ((k - i) % 2 ? P - 1 : 1ll) * C(k, i) % P * arr[x + i] % P;
        res = (res + coef) % P;
    }
    return res;
}
signed main() {
    scanf("%lld", &d);
    for(int i = 0; i <= d; i++) scanf("%lld", &a[i]);
    for(int i = 0; i <= d; i++) scanf("%lld", &b[i]);
    fac[0] = 1;
    for(int i = 1; i <= d; i++) fac[i] = fac[i - 1] * i % P;
    inv[d] = qpow(fac[d], P - 2);
    for(int i = d; i; i--) inv[i - 1] = inv[i] * i % P;
    int a0 = f(a, d - 1, 0), a1 = f(a, d - 1, 1);
    int b0 = f(b, d - 1, 0);
    printf("%lld\n", (b0 - a0 + P) * qpow(a1 + P - a0, P - 2) % P);
    debug("time=%.4lfs\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST